<?php
class Index extends Action {
	private $limit = 15;
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$data = array ();
		
		$start = rqst ( 'start', 1 ); // 分页
		$where = Model::where_build ( array ('uid as U.uid','uname like','name like','email like','status' => 0 ), $data, array ('uname' => 'like','email' => 'like','name' => 'like' ) );
		$data ['gid'] = irqst ( 'gid' );
		$userModel = new CoreUserModel ();
		$userModel->alias ( 'U' );
		if (! empty ( $data ['gid'] ) && is_numeric ( $data ['gid'] )) {
			$userModel->join ( 'core_groupuser AS CG', "U.uid = CG.uid", Model::INNER );
			$where ['CG.gid'] = $data ['gid'];
		}
		$userModel->sort ( array ('uid','d' ) );
		
		$users = $userModel->where ( $where )->limit ( $this->limit, $start - 1 )->count ( true )->retrieve ( "U.*" );
		
		$data ['limit'] = $this->limit;
		
		$data ['curl'] = unkeepargs ( $this->url, 'start' );
		
		if ($users) {
			$data ['users'] = $users;
			$data ['totalUser'] = $users->countTotal;
		}
		
		$data ['stas'] = array (0 => '<span class="label label-success">正常</span>','1' => '<span class="label">禁用</span>' );
		$gM = new CoreGroupModel ();
		$groups = $gM->where(array('type'=>'user'))->select ();
		$data ['groups'] = $groups;
		
		if ($groups) {
			$data ['group_options'] = $groups->toArray ( 'gid', 'name', array ('0' => '请选择用户组' ) );
		} else {
			$data ['group_options'] = array ('0' => '请选择用户组' );
		}
		bind ( 'get_user_options', array ($this,'get_user_options' ), 10, 2 );
		
		bind ( 'get_user_bench_options', array ($this,'get_user_bench_options' ) );
		
		bind ( 'user_belongs', array ($this,'user_belongs' ), 10, 2 );
		
		return new SmartyView ( $data, 'users/list' );
	}
	public function get_user_options($options, $user) {
		$options .= '<a title="编辑" href="./?Ctlr=EditUser&uid=' . $user ['uid'] . '"><i class="icon-edit"></i></a>';
		if(!$user['reserved']){
			if ($user ['status'] == '0') {
				$options .= '<a title="禁用" onclick="return confirm(\'确定要禁用该用户账户?\');" href="./?Ctlr=ActiveUser&status=1&uid=' . $user ['uid'] . '"><i class="icon-ban-circle"></i></a>';
			} else {
				$options .= '<a title="激活" onclick="return confirm(\'确定要激活该用户账户?\');" href="./?Ctlr=ActiveUser&status=0&uid=' . $user ['uid'] . '"><i class="icon-ok-circle"></i></a>';
			}
		}
		if (! $user ['reserved'] && icando('delete', 'user')) {
			$options .= '<a title="删除" onclick="return confirm(\'确定要删除该用户账户?\');" href="./?Ctlr=DelUser&uid=' . $user ['uid'] . '"><i class="icon-trash"></i></a>';
		}
		if(icando('grant','user')){
			$options .= '<a title="授权" data-content="' . $user ['name'] . '" class="grant-user" id="grant-USER-' . $user ['uid'] . '" href="' . ADMIN_URL . 'dashboard/?Ctlr=Grant&type=USER&uid=' . $user ['uid'] . '"><i class="icon-user"></i></a>';
		}
		return $options;
	}
	public function get_user_bench_options($options) {
		if(icando('delete', 'user')){
			$options .= '<li><a href="./?Ctlr=DelUser" id="menu-del-user"><i class="icon-trash"></i>删除</a></li>';
		}
		$options .= '<li><a href="./?Ctlr=ActiveUser&status=0" class="menu-active-user"><i class="icon-pencil"></i>激活</a></li>';		
		$options .= '<li><a href="./?Ctlr=ActiveUser&status=1" class="menu-active-user"><i class="icon-ban-circle"></i>禁用</a></li>';
		return $options;
	}
	public function user_belongs($lists, $user) {
		static $guM = null;
		if ($guM == null) {
			$guM = new CoreGroupUserModel ();
		}
		$uid = $user ['uid'];
		$groups = $guM->getGroups ( $uid );
		if ($groups) {
			foreach ( $groups as $g ) {
				$cls = 'label-info';
				if (empty($g ['name'])) {				
					$g ['name'] = '已删除的组';
					$cls = 'label-warning';
				}
				$lists .= "<span class=\"label {$cls} mg-r5\">{$g['name']}<a href=\"#{$uid}/{$g['group_id']}\" class=\"delete-from-group\"><i title=\"点击将用户从该组移除.\" class=\"icon-remove-sign icon-white\"></i></a></span>";
			}
		}
		return $lists;
	}
}